package java_thinking.unit_18_IO._18_1;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * @Desc:   创建一个叫SortedDirList的类，它具有一个可以接受文件路径信息，并能构建该路径下所有文件的排序目录
 *          的构造器，向这个类添加两个重载的list()方法：一个产生整个列表，另一个产生于其参数相匹配的列表的子集；
 * @author: cww
 * @DateTime: 2019-05-12 11:30
 */

public class SortedDirList {
    private File path;

    public SortedDirList() {
        path = new File(".");
    }

    public SortedDirList(File path) {
        this.path = path;
    }
    public String[] list() {
        String[] list = path.list();
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
        return list;
    }
    public String[] list(final String fn_regex) {
        String[] list = path.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(fn_regex);
            @Override
            public boolean accept(File dir, String name) {
                return pattern.matcher(name).matches();
            }
        });
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
        long total = 0;
        long fs;
        for (String s : list) {
            fs = new File(path, s).length();
            System.out.println(s + "," + fs + "byte(s)");
            total += fs;
        }
        System.out.println("=========");
        System.out.println(list.length + " file(s)," + total + " bytes");
        return list;
    }
}

class E02_SortedDirList {
    public static void main(String[] args) {
        SortedDirList sortedDirList = new SortedDirList();
        System.out.println(Arrays.asList(sortedDirList.list("test*.*")));
        System.out.println(Arrays.asList(sortedDirList.list()));
    }
}
